/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.mleiria.bo.dashboard;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.apache.log4j.Logger;
import pt.mleiria.dao.dashboard.AlertsDao;
import pt.mleiria.dao.dashboard.SemanaDao;
import pt.mleiria.vo.AlertVO;
import pt.mleiria.vo.Constants;

/**
 *
 * @author manuel
 */
@Stateless
public class AlertsEJB {

    private static final Logger LOG = Logger.getLogger(AlertsEJB.class);

    @PersistenceContext(unitName = "DomusService")
    private EntityManager em;

    @EJB
    private LoginEJB loginEjb;

    /**
     *
     * @return
     */
    public LoginEJB getLoginEjb() {
        return loginEjb;
    }

    public AlertVO[] getAllAlerts() {
        Query q = em.createQuery("select a from AlertsDao a ");
        List<AlertsDao> list = q.getResultList();
        return fillVO(list);
    }

    public String[] getNomesAlert() {
        Query q = em.createQuery("select distinct nome from AlertsDao a ");
        List<String> list = q.getResultList();
        return list.toArray(new String[list.size()]);
    }

    public String[] getDiasSemanaAlert() {
        Query q = em.createQuery("select distinct semanadao from AlertsDao a ");
        List<SemanaDao> list = q.getResultList();
        final String[] dias = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            dias[i] = list.get(i).getDia();
        }
        return dias;
    }

    public AlertVO[] getFilterAlerts(final String nome, final String diaSemana) {
        LOG.info("[nome;diaSemana][" + nome + ";" + diaSemana + "]");
        Query q;
        if (nome.equals(Constants.TODOS) && diaSemana.equals(Constants.TODOS)) {
            q = em.createQuery("select a from AlertsDao a");
        } else if (nome.equals(Constants.TODOS) && !diaSemana.equals(Constants.TODOS)) {
            q = em.createQuery("select a from AlertsDao a where a.semanadao.dia = :dia");
            q.setParameter("dia", diaSemana);
        } else if (!nome.equals(Constants.TODOS) && diaSemana.equals(Constants.TODOS)) {
            q = em.createQuery("select a from AlertsDao a where a.nome = :nome");
            q.setParameter("nome", nome);
        } else if (!nome.equals(Constants.TODOS) && !diaSemana.equals(Constants.TODOS)) {
            q = em.createQuery("select a from AlertsDao a where a.nome = :nome and a.semanadao.dia = :dia");
            q.setParameter("nome", nome);
            q.setParameter("dia", diaSemana);
        } else {
            throw new IllegalArgumentException("Parametros inesperados: [" + nome + ";" + diaSemana + "]");
        }
        final List<AlertsDao> list = q.getResultList();
        return fillVO(list);
    }
    
    public void deleteAlert(final int id){
        Query q = em.createQuery("delete from AlertsDao a where a.id = :id");
        q.setParameter("id", id);
        q.executeUpdate();
    }

    private AlertVO[] fillVO(final List<AlertsDao> list) {
        List<AlertVO> tmpList = new ArrayList<AlertVO>();
        for (AlertsDao alertsDao : list) {
            AlertVO a = new AlertVO();
            a.setActivo(alertsDao.getActivo());
            a.setDescricao(alertsDao.getDescricao());
            a.setDiaSemana(alertsDao.getSemanadao().getDia());
            a.setHora(alertsDao.getHora());
            a.setHoraFim(alertsDao.getHoraFim());
            a.setNome(alertsDao.getNome());
            a.setVoice(alertsDao.getVoice());
            a.setId(alertsDao.getId());
            tmpList.add(a);
        }
        return tmpList.toArray(new AlertVO[tmpList.size()]);
    }

    public boolean saveAlert(final String nome, final String descricao, final String diaSemana,
            final String hora, final String horaFim, final boolean activo, final boolean voiceAlert) {
        try {
            final AlertsDao ad = new AlertsDao();
            ad.setActivo(activo == true ? 1 : 0);
            ad.setDescricao(descricao);
            ad.setHora(hora);
            ad.setHoraFim(horaFim);
            ad.setNome(nome);

            SemanaDao sd = new SemanaDao();
            sd.setId(getSemanaId(diaSemana));
            ad.setSemanadao(sd);

            ad.setVoice(voiceAlert == true ? 1 : 0);
            //em.persist(sd);
            em.persist(ad);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public String[] getAllDiasSemana() {
        Query q = em.createQuery("select sd from SemanaDao sd ");
        List<SemanaDao> list = q.getResultList();
        final String[] dias = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            dias[i] = list.get(i).getDia();
        }
        return dias;
    }

    private int getSemanaId(final String dia) {
        Query q = em.createQuery("select sd from SemanaDao sd where sd.dia = :dia");
        q.setParameter("dia", dia.toLowerCase());
        return ((SemanaDao)q.getSingleResult()).getId();
    }

    public String[] getAllNames() {
        return loginEjb.getAllUserNames();
    }

}
